/**
 * 汪孟苹
 */
define(["jquery","bbcare","as/app/growth/growthList.servers","as/app/growth/growthList.ui"],function($,$bb,$servers,$ui) {
    var growthList = {};

    /**
     * 请求统计数据成功后的回调函数
     */
    var ajaxStatisticsCallBack = function(data){
        // 赋值
        var watchList = $(".monitor-report-list > div");
        watchList.eq(1).find(".monitor-report-val").html(data.dayTestPatients);
        watchList.eq(2).find(".monitor-report-val").html(data.totalTestPatients);
        watchList.eq(3).find(".monitor-report-val").html(data.abnormalTimes);
        watchList.eq(4).find(".monitor-report-val").html(data.totalTestTimes);
        $(".monitor-report-footing").find(".totalNum").html(data.totalTestTimes);
    }

    /**
     * 点击进入详情页
     */
    var onBabyCardClick = function(){
        var growthRecordId = $(this).attr("growthRecordId");
        var userId = $(this).attr("userId");
        var obj = {
            "userId":userId,
            "recordId":growthRecordId
        }
        if( obj.userId && obj.recordId ){
            $bb.openPage("growthDetail",obj);
        }
    }

    /**
     * 搜索
     */
    var onSearch = function(){
        var id = $(this).attr("id");
        if( id == "hisNoBtn" || id == "userNameBtn" || id == "phoneNoBtn"){
            $(this).prev().val("");
        }
        var params = $(this).data("params");
        params.page = 1;
        var hisNo = $.trim( $("#hisNo").val() );
        var userName = $.trim( $("#userName").val() );
        var phoneNo = $.trim( $("#phoneNo").val() );
        var gender = $("#gender input[name=gender]:checked").val();
        var birthdayBeginDate = $("#birthdayBeginDate").val();
        var birthdayEndDate = $("#birthdayEndDate").val();
        var evaluationBeginDate = $("#evaluationBeginDate").val();
        var evaluationEndDate = $("#evaluationEndDate").val();
        var arg = {
            "hisNo":hisNo,
            "userName":userName,
            "phoneNo":phoneNo,
            "gender":gender,
            "birthdayBeginDate":birthdayBeginDate,
            "birthdayEndDate":birthdayEndDate,
            "evaluationBeginDate":evaluationBeginDate,
            "evaluationEndDate":evaluationEndDate,
        };
        $.extend(params,arg);
        listInit(params);
    }

    /**
     * 请求生长发育评估患者list成功后的回调函数
     */
    var ajaxGrowthListCallBack = function(data,params){
        if(data && data.rows ){
            var pagesAll = Math.ceil(data.total / params.rows);
            $ui.list({
                id:"growthList",
                list:data.rows,
                rows:params.rows
            });
            // 翻页
            $bb.page.init({
                "id":"btn-toolbar",
                "pagesAll":pagesAll,
                "page":params.page,
                "params":params,
                "callback":listInit,
            });
            /* 点击随访患者卡片进入详情页 */
            $("#growthList .clickbabycard").unbind("click");
            $("#growthList .clickbabycard").click(onBabyCardClick);
        }
        $("#hisNoBtn,#userNameBtn,#phoneNoBtn").unbind("click");
        $("#hisNoBtn,#userNameBtn,#phoneNoBtn").data("params",params);
        $("#hisNoBtn,#userNameBtn,#phoneNoBtn").click(onSearch);
        $("#hisNo,#userName,#phoneNo").unbind("input propertychange");
        $("#hisNo,#userName,#phoneNo").data("params",params);
        $("#hisNo,#userName,#phoneNo").bind("input propertychange",onSearch);
        $("#birthdayBeginDate,#birthdayEndDate,#evaluationBeginDate,#evaluationEndDate,#gender").unbind("change");
        $("#birthdayBeginDate,#birthdayEndDate,#evaluationBeginDate,#evaluationEndDate,#gender").data("params",params);
        $("#birthdayBeginDate,#birthdayEndDate,#evaluationBeginDate,#evaluationEndDate,#gender").change(onSearch);
    }

    var listInit = function(params){
        $servers.ajaxGrowthList(params,ajaxGrowthListCallBack);
    }

    /**
     * 跳转到添加记录页面
     */
    var onAdd = function(){
        $bb.openPage("growthAdd");
    }

    /**
     * 初始化
     */
    growthList.init = function(){
        // 请求统计数据
        $servers.ajaxStatistics(ajaxStatisticsCallBack);
        // 添加记录
        $("#toAddInfo").unbind("click");
        $("#toAddInfo").click(onAdd);
        var rows = document.body.offsetWidth < 992 ? 2 : document.body.offsetWidth < 1200 ? 3 : 4;
        var params = {
            "page":1,
            "rows":rows*4
        };
        listInit(params);
    }

    return growthList;
});
